<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="isDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptFrom" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getManager">
          <el-option v-for="item in people" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="deptOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartment, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  components: {},
  props: {
    isDialog: {
      type: Boolean,
      default: false
    },
    nodeTree: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      const { depts } = await getDepartment() // 获取最新组织架构的数组 // 获取改父节点所有的子节点
      // console.log(depts)
      let isRepeat = false
      if (this.formData.id) { // 编辑模式
        console.log('进入编辑模式规则,名字')
        isRepeat = depts.filter(item => item.pid === this.nodeTree.id).some(item => item.name === value)
        isRepeat ? callback(new Error(`子部门已经存在${value}了`)) : callback()
      } else { // 新增模式
        isRepeat = depts.filter(item => item.pid === this.nodeTree.id).some(item => item.name === value)
        isRepeat ? callback(new Error(`子部门已经存在${value}了`)) : callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      let p = false
      if (this.formData.id) {
        console.log('进入编辑模式，code')
        p = depts.some(item => item.id !== this.formData.id && item.code === value)
      }
      p = depts.some(item => item.code === value)

      p ? callback(new Error(`编码已经存在${value}了`)) : callback()
    }
    return {
      people: [],
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkName }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCode }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    fn() {
      this.$emit('cancle')
    },
    async getManager() {
      this.people = await getEmployeeSimple()
    },
    // 获取部门详情
    async  getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    deptOk() { // 提交表单校验
      this.$refs.deptFrom.validate(async isOk => {
        if (isOk) { // 表示校验通过
          if (this.formData.id) {
            console.log('编辑状态')
            await updateDepartments(this.formData)
          } else {
            await addDepartments({ ...this.formData, pid: this.nodeTree.id }) // 调用接口
          }
          this.$emit('addDetps') // 调用父组件的方法，实现视图同步
          this.$emit('update:isDialog', false) // 关闭弹层
          this.$message.success(`${this.showTitle}完成`)
        }
      })
    },
    btnCancel() {
      // 重置数据  因为resetFields 只能重置 表单上的数据 非表单上的 比如 编辑中id 不能重置
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptFrom.resetFields() // 重置校验字段
      this.$emit('update:isDialog', false) // 关闭
    }
  }
}

</script>

<style lang='less' scoped>
</style>
